<?php

require('config.php');

if(!isset($_SESSION['id_user']))
    redirectFront('connexion');

$page = new rootLayoutCategory();
$page->setBaliseTitle('profil_pseudo_balise');
$page->setDescription('profil_pseudo_description');
$page->addJavaScript('jQuery.form.validation.js');

//-- menu Left show products--------------------------------//
$menu = new layoutMenu();
$menuCategorie = new layoutMenuCategorie();
$menu->pushMenu($menuCategorie);
$menuPage = new layoutMenuPage();
$menu->pushMenu($menuPage);
$page->setMenu($menu);
if(!isset($_GET['id']) || $_GET['id']<0 || $_GET['id'] == ''){
	$msg = new Message('Cannot modify this password', Message::INFO);
    		Persistence::register(PERSIST_MESSAGE, $msg);
    		redirectFront('profil');
}

$id_infos = $_GET['id'];
//$user = modelUser::getProfilByTwoID($_SESSION['id_user'],$id_infos);

$user = modelUser::getUser($_SESSION['id_user']);
$nickname = modelUser::getProfilByIdInfos($id_infos);
if($id_infos<=0 && $id_infos==''){
	$nickname = modelUser::getProfil($_SESSION['id_user']);
}

if($nickname->id_user==$_SESSION['id_user']){
	if(isset($_POST['submit'])) {
	    try {
		        $timestamp = $_SESSION['profil_pass_timestamp'];
		        session_unregister('profil_pass_timestamp');
		        $apass = trim($_POST['apass_'.$timestamp]);
		        $pass  = trim($_POST['pass_'.$timestamp]);
		        $cpass = trim($_POST['cpass_'.$timestamp]);
				/*
		        if($pass=='' or $cpass=='')
		            throw new Exception('Vous devez remplir toutes les informations demandées');
		            */
	        	if(!modelUser::verifPseudoPass($id_infos,$apass)){		        		
		            $msg = new Message('Votre ancien mot de passe ne correspond pas', Message::INFO);
		        	Persistence::register(PERSIST_MESSAGE, $msg);
				}else{
			        modelUser::updatePseudoPass($id_infos, $pass);
			        $msg = new Message('Votre mot de passe a été changé', Message::INFO);
			        Persistence::register(PERSIST_MESSAGE, $msg);
			        redirectFront('profil','id='.$nickname->id_infos);
				}

	    }
	    catch(Exception $e) {
	    	echo $e;
	        $msg = new Message($e->getMessage(), Message::AVERT);
	        Persistence::register(PERSIST_MESSAGE, $msg);

	    }
	}
}else{

redirectFront('profil');
} 
//-- corps de la page -----------//
$tpl_file='profil-pseudo-pass.tpl';
$profil_pass = new flyLayout(REP_TPL . $tpl_file);
$profil_pass->start();

if($nickname->pass_word){
	$profil_pass->showBlock('old_password');
}

$tabs = new layoutTabs();
$tabs->pushLink('Mon compte', '/profil.php');
$tabs->pushLink('Mes messages', '/profil-message.php');
$profil_pass->includeLayout('tabs', $tabs);

$_SESSION['profil_pass_timestamp'] = time();
$profil_pass->setVariable('timestamp', $_SESSION['profil_pass_timestamp']);
$profil_pass->stop();
$page->setBody($profil_pass);

$page->display();

?>